let it = makeIterator([1,2,3,4]);

console.dir(it.next());
console.dir(it.next());
console.dir(it.next());
console.dir(it.next());
console.dir(it.next());

function makeIterator(list) {
    let nextIndex = 0;
    return {
        next: function() {
            if (nextIndex < list.length) {
                let res = ({
                    value: list[nextIndex],
                    done: false,
                });
                nextIndex += 1;
                return res;
            } else {
                return ({
                    value: undefined,
                    done: true,
                });
            }
        }
    }
}

// 只要有 Symbol.iterator 这个属性的对象都会被认为是可以迭代的
// list 原生自带这个方法

let x = [5,6,7,8];
console.dir(x[Symbol.iterator]);

for (let i of x) {
    console.dir(i);
}
